package com.maxlen.goods.controller;

import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.R;
import com.maxlen.common.valid.AddValid;
import com.maxlen.common.valid.UpdateValid;
import com.maxlen.goods.entity.vo.AttrGroupVO;
import com.maxlen.goods.entity.dto.AttrGroupDTO;
import com.maxlen.goods.entity.dto.AttrGroupWithAttrsDTO;
import com.maxlen.goods.service.AttrGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author MaxLen
 * @email MaxLen@gmail.com
 * @date 2022-10-27 10:12:58
 */
@RestController
@RequestMapping("goods/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @GetMapping("/list/{catId}")
    // @RequiresPermissions("goods:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catId){
        PageUtils page = attrGroupService.queryPage(params, catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    // @RequiresPermissions("goods:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupDTO attrGroup = attrGroupService.getDetailById(attrGroupId);
        return R.ok().put("data", attrGroup);
    }

    /**
     * 获取分类下所有分组和关联属性
     * @param categoryId
     * @return
     */
    @GetMapping("/with/attr/{categoryId}")
    public R getLIstWithAttr(@PathVariable("categoryId") Long categoryId){
        List<AttrGroupWithAttrsDTO> attrGroupWithAttrsDTOS = attrGroupService.getAttrGroupWithAttrsByCategoryId(categoryId);
        return R.ok().put("data", attrGroupWithAttrsDTOS);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("goods:attrgroup:save")
    public R save(@Validated(AddValid.class) @RequestBody AttrGroupVO attrGroupVO){
		attrGroupService.add(attrGroupVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    // @RequiresPermissions("goods:attrgroup:update")
    public R update(@Validated(UpdateValid.class) @RequestBody AttrGroupVO attrGroupVO){
		attrGroupService.updateByIdDto(attrGroupVO);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("goods:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeDetail(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
